package com.springboot.admin.service.impl;

import com.springboot.admin.entity.WebStatsDto;
import com.springboot.admin.mapper.PagesDao;
import com.springboot.admin.mapper.WebStatsDao;
import com.springboot.admin.service.WebStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class WebStatsServiceImpl implements WebStatsService {

  @Autowired
  WebStatsDao webStatsDao;

  @Autowired
  PagesDao pagesDao;

  public void insertWebStats(WebStatsDto webStatsDto) {
    webStatsDao.insertWebStats(webStatsDto);
  }

  public Map getWebStatistics() {
    // 获取当天时间
    // Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //  String currDate = (String) sdf.format(d);

    // 获取当天时间
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());
    calendar.add(Calendar.DATE,0);
    String lastDate = (String) sdf.format(calendar.getTime());

    // 获取前一天时间
    Calendar calendar2 = new GregorianCalendar();
    calendar2.setTime(new Date());
    calendar2.add(Calendar.DATE,-1);
    String lastTwoDate = (String) sdf.format(calendar2.getTime());

    // 获取前两天时间
    Calendar calendar3 = new GregorianCalendar();
    calendar3.setTime(new Date());
    calendar3.add(Calendar.DATE,-2);
    String lastThreeDate = (String) sdf.format(calendar3.getTime());

    Map map = new HashMap();
    // 查所有访问量
    map.put("allViews", pagesDao.getCountViews());

    Map lastDayViews = webStatsDao.getSomeDayViews(lastDate);
    Map twoDayagoViews = webStatsDao.getSomeDayViews(lastTwoDate);

    // 访问量
    int twoDayagoNum = ((int)lastDayViews.get("allViews") - (int)twoDayagoViews.get("allViews")) ;
    map.put("allViewsMom", twoDayagoNum);

    // 查所有文章
    map.put("allpages", pagesDao.getAllCount());
    int twoDayagoNum2 = ((int)lastDayViews.get("allpages") - (int)twoDayagoViews.get("allpages")) ;
    map.put("allpagesMom", twoDayagoNum2);

    // 查当天数据
    map.put("dayViews",webStatsDao.getViewsByDate());
    int twoDayagoNum3 = ((int)lastDayViews.get("dayViews") - (int)twoDayagoViews.get("dayViews")) ;
    map.put("dayViewsMom",twoDayagoNum3);

    // 查当天Ip
    map.put("dayIp",webStatsDao.getViewsByIp());
    int twoDayagoNum4 = ((int)lastDayViews.get("dayIp") - (int)twoDayagoViews.get("dayIp")) ;
    map.put("dayIpMom",twoDayagoNum4);

    // 查所有设备类型
    map.put("deviceType", webStatsDao.getDeviceType());
    // 查所有分辨率
    map.put("deviceRatio", webStatsDao.getDeviceRatio());
    // 查所有浏览器类型
    map.put("browserType", webStatsDao.getBrowserType());
    // 查看每天浏览量
    map.put("everyDayViews", webStatsDao.getEveryDayViews());
    return map;
  }
}